package cn.quevo.cube.provide.define.entity;

import cn.quevo.common.entity.DoEntity;
import cn.quevo.common.enums.ResultStatusEnum;
import cn.quevo.common.exception.CheckParamsException;
import cn.quevo.common.lang.EnumUtils;
import cn.quevo.common.lang.StringUtils;
import cn.quevo.cube.provide.define.enums.DateFormat;
import cn.quevo.cube.provide.define.enums.SequenceType;
import lombok.*;
import org.beetl.sql.core.annotatoin.Table;

/**
 * 字段序列 实体BEAN
 *
 * @author July july_sky@foxmail.com
 * @version 1.0
 * @date 2018/12/11 14:56:58
 * @Copyright 湘豫（北京）科技有限公司版权所有.All Rights Reserved.
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Table(name = "cube_column_sequence")
public class CubeColumnSequence extends DoEntity {

    /** 序列类型:AUTO_自增,DATE_日期 */
    private String sequenceTypeCd;
    /** 模型ID */
    private Long modelId;
    /** 字段iD */
    private Long columnId;
    /** 序列标签(中文名称) */
    private String sequenceLabel;
    /** 序列名称(英文名称) */
    private String sequenceName;
    /** 日期格式(yyyyMMdd/yyyyMMddHH24/yyyyMMddHH24MI/yyyyMMddHH24MiSS/yyyyMMddHH24MISSMS) */
    private String formatter;
    /** 起始值 */
    private Long startValue;
    /** 当前值 */
    private Long currentValue;
    /** 当前日期值 */
    private String currentDateValue;
    /** 增量 */
    private Long stepInt;

    /**
    * 公共参数验证
    */
    protected void validate() {
        if(StringUtils.isBlank(this.getSequenceLabel())){
            throw new CheckParamsException(6004, "序列标签不能为空");
        }

        this.setStepInt(StringUtils.isBlank(this.getStepInt()) ? 1 : this.getStepInt());

    }

    /**
    * 新增参数验证
    */
    public void validateCreate() {
        if (StringUtils.isBlank(this.getSequenceTypeCd()) || !EnumUtils.existsEnumIgnoreCase(this.getSequenceTypeCd(), SequenceType.values())) {
            throw new CheckParamsException(6001, "序列类型错误");
        }
        if(StringUtils.isBlank(this.getColumnId())){
            throw new CheckParamsException(6003, "字段不能为空");
        }
        if(StringUtils.isBlank(this.getSequenceName()) || StringUtils.isBlank(this.getSequenceName())){
            throw new CheckParamsException(6005, "序列名称不能为空且只能包含英文和下划线");
        }
        if (this.getSequenceTypeCd().equalsIgnoreCase(SequenceType.DATE.getCode())) {
            if(!EnumUtils.existsEnumIgnoreCase(this.getFormatter(),DateFormat.values())){
                throw new CheckParamsException(6006, "日期格式错误");
            }
        }else {
            this.setStartValue(StringUtils.isBlank(this.getStartValue()) ? 1 : this.getStartValue());
            this.setCurrentValue(StringUtils.isBlank(this.getCurrentValue()) ? 1 : this.getCurrentValue());

        }
        this.validate();
    }

    /**
    * 修改参数验证
    */
    public void validateUpdate() {
        if (StringUtils.isBlank(this.getId())) {
            throw new CheckParamsException(ResultStatusEnum.RECORD_UNSELECTED);
        }
        this.validate();
    }
}
